package com.message.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/10/25 14:41
 **/
public class RabbitMqConfig {

    public static final String QUEUE_INFORM_REDIS = "queue_inform_redis";
    public static final String QUEUE_INFORM_ES = "queue_inform_es";
    public static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
    public static final String ROUTINGKEY_REDIS="inform.#.redis.#";
    public static final String ROUTINGKEY_ES="inform.#.es.#";

    //声明交换机
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM(){
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //声明QUEUE_INFORM_REDIS队列
    @Bean(QUEUE_INFORM_REDIS)
    public Queue QUEUE_INFORM_EMAIL(){
        return new Queue(QUEUE_INFORM_REDIS,true);
    }
    //声明QUEUE_INFORM_ES队列
    @Bean(QUEUE_INFORM_ES)
    public Queue QUEUE_INFORM_SMS(){
        return new Queue(QUEUE_INFORM_ES,true);
    }

    //ROUTINGKEY_REDIS队列绑定交换机，指定routingKey
    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_REDIS) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_REDIS).noargs();
    }
    //ROUTINGKEY_ES队列绑定交换机，指定routingKey
    @Bean
    public Binding BINDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_ES) Queue queue,
                                          @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ES).noargs();
    }

}
